package cn.tedu.hotel.serve.service.impl;

import cn.tedu.hotel.serve.ex.ServiceException;
import cn.tedu.hotel.serve.mapper.ThemeMapper;
import cn.tedu.hotel.serve.pojo.dto.ThemeImgDTO;
import cn.tedu.hotel.serve.pojo.dto.ThemeImgUpdateDTO;
import cn.tedu.hotel.serve.pojo.entity.Theme;
import cn.tedu.hotel.serve.pojo.entity.ThemeImg;
import cn.tedu.hotel.serve.pojo.vo.ThemeImgVO;
import cn.tedu.hotel.serve.service.IThemeService;
import cn.tedu.hotel.serve.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Slf4j
@Service
public class ThemeServiceImpl implements IThemeService {
    @Value("${dirPath}")
    private String dirPath;

    @Autowired
    ThemeMapper themeMapper;

    @Override
    public List<Theme> getTheme() {
        log.debug("开始处理【后台返回主题住宿】的业务");
        List<Theme> theme = themeMapper.getTheme();
        log.debug("数据：{}",theme);
        return theme;
    }

    @Override
    public void addThemeImg(ThemeImgDTO themeImgDTO) {
        log.debug("开始处理【后台插入主题住宿图片】的业务,参数：{}",themeImgDTO);
        int rows = themeMapper.addThemeImg(themeImgDTO);
        if (rows!=1){
            String message = "插入图片失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, "插入图片失败，服务器忙，请稍后再次尝试！");
        }
    }

    @Override
    public ThemeImgVO getThemeImgById(Long id) {
        return themeMapper.getThemeImgById(id);
    }
    @Override
    public List<ThemeImgVO> getThemeImgList() {
        log.debug("开始处理【查询后台主题住宿图片】的业务");
        return themeMapper.getThemeImgList();
    }

    @Override
    public List<ThemeImgVO> getThemeImgListByThemeNameSelectId(String themeName) {
        log.debug("开始处理【查询前台主题住宿图片】的业务,参数：{}",themeName);
        Long idByName = themeMapper.getIdByName(themeName);
        if (idByName==null){
            String message = "查询主题住宿，主题住宿id不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        List<ThemeImgVO> themeImgListByThemeId = themeMapper.getThemeImgListByThemeId(idByName);
        return themeImgListByThemeId;
    }
    @Override
    public void delete(Long id) {
        log.debug("ThemeService开始处理[根据ID删除主题相册的业务],参数{}", id);
        ThemeImgVO themeImgVO = themeMapper.getThemeImgById(id);

        log.debug("根据ID={}检查主题相册数据是否存在，查询结果：{}", id, themeImgVO);

        //判断是否存在
        if (themeImgVO == null) {
            //是,抛出异常
            String message = "删除主题相册失败，尝试删除的主题相册数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);

        String url = themeImgVO.getUrl();
        log.debug("图片路径:" + dirPath + url);
        new File(dirPath + url).delete();

        themeMapper.deleteById(id);
    }

    @Override
    public void update(Long id, ThemeImgUpdateDTO themeImgUpdateDTO) {
        log.debug("ThemeService开始处理[根据ID修改主题相册的业务],参数{}", id);
        ThemeImgVO themeImgVO = themeMapper.getThemeImgById(id);

        log.debug("根据ID={}检查主题相册数据是否存在，查询结果：{}", id, themeImgVO);
        //判断是否存在
        if (themeImgVO == null) {
            //是,抛出异常
            String message = "删除主题相册失败，尝试删除的主题相册数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        // 调用Mapper对象的update()方法执行修改
        log.debug("即将执行修改，参数：{}", id);
        log.debug("themeImgUpdateDTO的url：{}", themeImgUpdateDTO.getUrl());
        ThemeImg themeImg = new ThemeImg();
        BeanUtils.copyProperties(themeImgUpdateDTO,themeImg);
        themeImg.setId(id);
        log.debug("url:{}", themeImg.getUrl());
        themeMapper.update(themeImg);
    }
}
